package cn.jbolt.core.gen;

import cn.jbolt.core.db.datasource.JBoltDataSourceUtil;
import cn.jbolt.core.db.datasource.JBoltDatasource;
import cn.jbolt.core.db.dialect.*;
import cn.jbolt.core.db.sql.DBType;
import cn.jbolt.core.util.JBoltConsoleUtil;
import com.jfinal.plugin.activerecord.generator.Generator;
import com.jfinal.plugin.activerecord.generator.TableMeta;

import javax.sql.DataSource;
import java.util.List;

/**
 * 生成器主体
 *
 * @ClassName: JBoltGenerator
 * @author: JFinal学院-小木 QQ：909854136
 * @date: 2019年12月11日
 */
public class JBoltGenerator extends Generator {
	private JBoltMetaBuilder metaBuilder;

	public JBoltGenerator(String dataSourceConfigName, DataSource dataSource, String dataDictionaryVersion,
			String dataDictionaryDescription) {
		super(dataSource, new JBoltBaseModelGenerator(dataSourceConfigName,JBoltProjectGenConfig.baseModelPackageName,
				JBoltProjectGenConfig.baseModelOutputDir));
		if (JBoltProjectGenConfig.genModel) {
			this.modelGenerator = new JBoltModelGenerator(dataSourceConfigName, JBoltProjectGenConfig.dbType,
					JBoltProjectGenConfig.idGenMode, JBoltProjectGenConfig.modelPackageName,
					JBoltProjectGenConfig.baseModelPackageName, JBoltProjectGenConfig.modelOutputDir);
		}
		this.setDataDictionaryGenerator(
				new JBoltDataDictionaryGenerator(dataSource, JBoltProjectGenConfig.modelOutputDir));


		String schemaPattern = null;
		switch (JBoltProjectGenConfig.dbType) {
		case DBType.MYSQL:
			setDialect(new JBoltMysqlDialect());
			metaBuilder = new JBoltMetaBuilder(dataSource);
			break;
		case DBType.ORACLE:
			setDialect(new JBoltOracleDialect());
			metaBuilder = new _JBoltOracleMetaBuilder(dataSource);
			break;
		case DBType.SQLSERVER:
			setDialect(new JBoltSqlServerDialect());
			metaBuilder = new _JBoltSqlServerMetaBuilder(dataSource);
			break;
		case DBType.POSTGRESQL:
			setDialect(new JBoltPostgresqlDialect());
			metaBuilder = new JBoltMetaBuilder(dataSource);
			break;
		case DBType.DM:
			setDialect(new JBoltDMDialect());
			metaBuilder = new JBoltMetaBuilder(dataSource);
			break;
		}
		if (metaBuilder == null) {
			throw new RuntimeException("目前只支持Mysql、Oracle、SqlServer、Postgresql、达梦 5个数据库");
		}
		schemaPattern = getSchema(dataSourceConfigName);
		if (schemaPattern != null) {
			metaBuilder.setSchemaPattern(schemaPattern);
		}
		metaBuilder.setGenerateRemarks(true);
		metaBuilder.skip(JBoltProjectGenConfig.tableSkipPredicate);
		setMetaBuilder(metaBuilder);

	}

	private String getSchema(String dataSourceConfigName) {
		JBoltDatasource jBoltDatasource = JBoltDataSourceUtil.me.getJBoltDatasource(dataSourceConfigName);
		if(jBoltDatasource!=null){
			return  jBoltDatasource.getDbSchema();
		}
		return null;
	}

	@Override
	public void generate() {
		JBoltConsoleUtil.printJboltcn();
		JBoltConsoleUtil.printMessage(
				"=========================JBolt Generator:JFinal Model Generator:Start=========================");
		JBoltConsoleUtil.printMessageWithDate(" JBolt Generate Start");
		if (dialect != null) {
			metaBuilder.setDialect(dialect);
		}

		long start = System.currentTimeMillis();
		List<TableMeta> allMatas = metaBuilder.build();
		if (allMatas.size() == 0) {
			JBoltConsoleUtil.printMessageWithDate(" TableMeta 数量为 0，不生成任何文件");
			return;
		}
//		dataDictionaryGenerator.generate(allMatas);
		if (JBoltProjectGenConfig.genModel) {
			JBoltConsoleUtil.printMessageWithDate("检测需要生成model和baseModel");
			baseModelGenerator.generate(allMatas);
			modelGenerator.generate(allMatas);
		}
		if (mappingKitGenerator != null) {
			mappingKitGenerator.generate(allMatas);
		}
		long usedTime = (System.currentTimeMillis() - start) / 1000;
		JBoltConsoleUtil.printMessageWithDate(" JBolt Generate complete in " + usedTime + " seconds.");
		JBoltConsoleUtil.printMessage(
				"=========================JBolt Generator:JFinal Model Generator:Done=========================");
	}

}
